<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="问卷名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" :maxLength="100" placeholder="请输入问卷名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问卷介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-textarea v-model="model.content" :maxLength="255" :autosize="{ minRows: 4 }" placeholder="请输入问卷介绍" />
              <div style="text-align: right;margin:0;line-height: 20px;" v-if="!formDisabled">{{model.content?model.content.length :0}}/255</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问卷链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
              <a-input v-model="model.url" :maxLength="255" placeholder="请输入问卷链接"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24"  v-if="sourceShow">
            <a-form-model-item label="问卷来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="source">
              <j-dict-select-tag type="list" v-model="model.source" dictCode="questionnaire_source" placeholder="请选择问卷来源" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="截止时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date :showTime="true"  date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择截止时间" v-model="model.endTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" :maxLength="255" :autosize="{ minRows: 4 }" placeholder="请输入备注" />
              <div style="text-align: right;margin:0;line-height: 20px;" v-if="!formDisabled">{{model.remark?model.remark.length :0}}/255</div>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import Vue from 'vue'
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { rules } from "@/utils/rules.js";
  import { USER_INFO } from '@/store/mutation-types'
  function validateUrl  (rule, value, callback) {
    let reg = rules.url
    if(!value && value!=='0'){
      callback()
    }else{
      if (!reg.test(value)) {
        callback(rules.urlMsg)
      } else {
        callback()
      }
    }
  };
  export default {
    name: 'SkQuestionnaireForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入问卷名称!'},
           ],
           content: [
              { required: true, message: '请输入问卷介绍!'},
           ],
           source: [
              { required: true, message: '请选择问卷来源!'},
           ],
           endTime: [
              { required: true, message: '请选择截止时间!'},
           ],
           url: [
              { required: true, message: '请输入问卷链接!'},
              { validator: validateUrl,trigger: 'blur' },
           ],
        },
        url: {
          add: "/family/skQuestionnaire/add",
          edit: "/family/skQuestionnaire/edit",
          queryById: "/family/skQuestionnaire/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      TenantIds(){
        return this.$store.state.skCommon.relTenantIds
      },
      sourceShow(){
        return this.TenantIds.indexOf(Vue.ls.get(USER_INFO).relTenantIds) != -1?false:true
        // return this.TenantIds == Vue.ls.get(USER_INFO).relTenantIds?false:true
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            if (!this.sourceShow) {
              this.model.source = "1";
            }
            that.confirmLoading = true;
            this.$emit("modelLoading",that.confirmLoading)
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.$emit("modelLoading",that.confirmLoading)
            })
          }
         
        })
      },
    }
  }
</script>